<?php
namespace app\common\logic;
use think\Db;
use think\Exception;

class KsPayment
{
    private static $_instance;
    
    private function __construct() {}
    
    public static function getInstance()
    {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function dealInfo($data) {
        $kjjg_arr = str_split(trim($data['kjjg']),1);
        $kjjg_1 = $kjjg_arr[0];
        $kjjg_2 = $kjjg_arr[1];
        $kjjg_3 = $kjjg_arr[2];
        $zjzs = 0;
        $zjje = 0;
		$is_new = 0; //是否后期合并投注信息
	try{
        switch($data['wfid']) {
			case '290':
				/**@todo 三不同号拖胆*/
				$tzhm = str_replace(" ","",$data['tzhm']);
				$tzhm_attr = explode(',',$tzhm);
				$tzhm_dw = str_split($tzhm_attr[0]);
				$tzhm_tw = str_split($tzhm_attr[1]);
				$dw = array_intersect($kjjg_arr,$tzhm_dw);
				$tw = array_intersect($kjjg_arr,$tzhm_tw);
				if( (1==count($tzhm_dw) && 1==count($dw) && 2==count($tw) ) || (2==count($tzhm_dw) && 2==count($dw) && 1==count($tw)) ){
					$zjzs = 1;
				}
				break;
        	case '97':
        		$hmarcshdu=str_replace(" ","",$data['tzhm']);
        		for($k_1=0;$k_1<strlen($hmarcshdu);$k_1++){
        			for($k_2=0;$k_2<strlen($hmarcshdu);$k_2++){
        				$hmq_1=substr($hmarcshdu,$k_1,1);
        				$hmq_2=substr($hmarcshdu,$k_2,1);
        				if($hmq_1!=$hmq_2){
        					$hm_jg=$hmq_1.$hmq_2;
        					if(strpos($data['kjjg'],$hm_jg)!== false){
        						$zjzs=$zjzs+1;
        					}
        				}
        			}
        		}
        	    break;
        	case '98':
        		$hmarcshdu=str_replace(" ","",$data['tzhm']);
        		$hmaray=explode(",",$hmarcshdu);
        		if(count($hmaray)==2){
        			for($k=0;$k<strlen($hmaray[0]);$k++){
        				for($k_1=0;$k_1<strlen($hmaray[1]);$k_1++){
        					$hmq_1=substr($hmaray[0],$k,1);
        					$hmq_2=substr($hmaray[1],$k_1,1);
        					if($hmq_1 != $hmq_2) {
        						if((strpos($data['kjjg'],$hmq_1) !== false) &&(strpos($data['kjjg'],$hmq_2) !== false)) {
        							$zjzs += 1;
        						}
        					}
        				}
        			}
        		}
        		break;
        	case '99':
        		$hmarcshdu=str_replace(" ","",$data['tzhm']);
        		$hmaray=explode(",",$hmarcshdu);
        		if(count($hmaray)==2){
        			for($k=0;$k<strlen($hmaray[0]);$k++){
        				for($k_1=0;$k_1<strlen($hmaray[1]);$k_1++){
        					if($k%2==0){
        						$touzhhm=substr($hmaray[0],$k,2).substr($hmaray[1],$k_1,1);
        						$touzhhm_1=substr($hmaray[1],$k_1,1).substr($hmaray[0],$k,2);
        						if($data['kjjg']==$touzhhm || $data['kjjg']==$touzhhm_1){
        							$zjzs=$zjzs+1;
        						}
        					}
        				}
        			}
        		}	 
        		break;
        	case '100':
//        		$hmarcshdu=str_replace(" ","",$data['tzhm']);
//        		for($k=0;$k<strlen($hmarcshdu);$k++){
//        			if($k%3==0){
//        				$chouchuhm=substr($hmarcshdu,$k,2);
//        				if(strstr($data['kjjg'],$chouchuhm)){
//        					$zjzs=$zjzs+1;
//        				}
//        			}
//        		}
				$tz_list = explode( ' ',trim($data['tzhm']) );
				$count = count(array_unique($kjjg_arr));
				if( 2==$count ){
					foreach( $tz_list as $v ){
						$v_attr = str_split(trim($v),1);
						$v_count = count(array_unique($v_attr));
						$int = array_intersect($kjjg_arr,$v_attr);
						if( 2==count($int) && 1==$v_count ){
							$zjzs += 1;
						}
					}
				}
        		break;
        	case '101':
        		if($kjjg_1!=$kjjg_2 && $kjjg_1!=$kjjg_3 && $kjjg_2!=$kjjg_3){
        			if(strpos($data['tzhm'],$kjjg_1)!== false && strpos($data['tzhm'],$kjjg_2)!== false && strpos($data['tzhm'],$kjjg_3)!== false){
        				$zjzs=$zjzs+1;
        			}
        		}
        		break;
        	case '102':
				$tz_list = explode( ' ',trim($data['tzhm']) );
				$kj = trim( $data['kjjg'] );
				foreach($tz_list as $v){
					$v==$kj && $zjzs += 1;
				}
        		break;
        	case '103':
        		if(strpos($data['tzhm'], $data['kjjg'])!== false && $kjjg_1==$kjjg_2 && $kjjg_1==$kjjg_3 && $kjjg_2==$kjjg_3){
    				$zjzs=$zjzs+1;
    			}
        		
        		break;
        	case '104':
        		if($data['kjjg']=="123" || $data['kjjg']=="234" || $data['kjjg']=="345" || $data['kjjg']=="456"){
		         	if(strpos($data['tzhm'], $data['kjjg'])!== false){
						 $zjzs=$zjzs+1;
					 }
				 }
        		break;
        	case '105':
				$is_new = 1;
				$pl_list = explode( '|',trim($data['dqpl']) );
				$tz_list = explode( ' ',trim($data['tzhm']) );
				$sum = intval($kjjg_1) + intval($kjjg_2) + intval($kjjg_3);

				foreach( $tz_list as $k=>$v ){
					if( '小' == $v && $sum > 2 && $sum < 11 ){
						$zjzs += 1;
						$zjje += (floatval($data['dzje']) * floatval($pl_list[$k]));
					}elseif( '大' == $v && $sum > 10 && $sum < 19 ){
						$zjzs += 1;
						$zjje += (floatval($data['dzje']) * floatval($pl_list[$k]));
					}elseif( '单' == $v && 1==$sum%2 ){
						$zjzs += 1;
						$zjje += (floatval($data['dzje']) * floatval($pl_list[$k]));
					}elseif( '双' == $v && 0==$sum%2 ){
						$zjzs += 1;
						$zjje += (floatval($data['dzje']) * floatval($pl_list[$k]));
					}elseif( is_numeric($v) && intval($v)==$sum ){
						$zjzs += 1;
						$zjje += (floatval($data['dzje']) * floatval($pl_list[$k]));
					}
				}

        		break;
        }
	}catch (Exception $e){
		echo $e->getMessage() ;
		return false;
	}
        //结算中奖并转移结算数据
        if($data['nb']==1 || $data['swzh']==1) {
            $table = 'touzhu_list_sw';
        } else {
            $table = 'touzhu_list_ok';
        }
        
        //选择投注模式
        if($data['nbzh'] == 1) $pieces = 1;
        if($data['nbzh'] == 2) $pieces = 10;
        if($data['nbzh'] == 3) $pieces = 100;
        
        //处理中奖金额（赔率*单注金额*中奖注数/投注模式），特殊玩法除外
		if( 1==$is_new ){
			$zjje = $zjje / $pieces;
		}else{
			$zjje = $data['dqpl'] * $data['dzje'] * $zjzs / $pieces;
		}

        //转移投注注单
        $insert_data = $data;
		$is_zh = $insert_data['from'];
        unset($insert_data['user_id'],$insert_data['swzh'],$insert_data['nb'],$insert_data['rktime'],$insert_data['cp_name_old'],$insert_data['cp_name_new'],$insert_data['from']);
        $insert_data['ifqs'] = '1';
        $insert_data['zjje'] = $zjje;
        $insert_data['zjzs'] = $zjzs;
        
        $insert_pc = $insert_data;
        unset($insert_pc['tzhje'],$insert_pc['tz_time']);
        $tzhje = Db::name('username')->where('id',$data['user_id'])->value('je');
        $insert_pc['tzhje'] = $tzhje;
        $insert_pc['tz_time'] = time();
        
        Db::startTrans();
        try {
        	if(!Db::name($table)->data($insert_data)->insert()){
        		throw new \Exception(false);
        	}
        	
        	if(!Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete()){
        		throw new \Exception(false);
        	}
        	
        	//中奖金额大于0.001则给用户增加金额
        	if($zjje > 0.001) {
        		if(!Db::name('touzhu_list_pc')->data($insert_pc)->insert()){
        			throw new \Exception(false);
        		}
        		//$prefix = config('database.prefix');
        		//Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
        		if(!Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]])){
        			throw new \Exception(false);
        		}

				//处理追号自动停止
				if( 1==intval($is_zh) ){
					$list_zh = Db::name('touzhu_list_zh')->field('*,1 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'stop'=>1])->select();
					$list_no = Db::name('touzhu_list_no')->field('*,2 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'from'=>1])->select();
					$list = array_merge($list_zh,$list_no);
                    $tkqje = Db::name('username')->where('id',$data['user_id'])->value('je');
					//$tkqje = floatval($tzhje) + floatval($zjje);
					foreach($list as $v){
						$id = $v['id'];
						unset($v['id']);
						if( 1==$v['type'] ){
							unset($v['type']);
							unset($v['stop']);
							if( !Db::name('touzhu_list_zh')->delete($id) ){
								throw new \Exception(false);
							}
						}else{
							unset($v['rktime']);
							unset($v['type']);
							unset($v['from']);
							if( !Db::name('touzhu_list_no')->delete($id) ){
								throw new \Exception(false);
							}
						}
						$v['tktime'] = time();
						$v['tkqje'] = $tkqje;
						$tkqje += floatval($v['tzje']);

						if( !Db::name('touzhu_list_tk')->insert($v) ){
							throw new \Exception(false);
						}
						if( !Db::name('username')->where('id',$data['user_id'])->update(array('je'=>['exp',"je+'{$v['tzje']}'"],'tzzje'=>['exp',"tzzje-'{$v['tzje']}'"])) ){
							throw new \Exception(false);
						}
					}
				}
        	}
           /*  Db::name($table)->data($insert_data)->insert();
            Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete();
            //中奖金额大于0.001则给用户增加金额
            if($zjje > 0.001) {
                //$prefix = config('database.prefix');
                //Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
                Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]]);
            } */
            Db::commit();
            return $zjje;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
        
    }
}